/* 
 * File:   GaussianExtractorRGB.cpp
 * Author: hadrien
 * 
 * Created on 22 avril 2012, 04:13
 */

#include <vector>

#include "GaussianExtractorRGB.h"

using namespace cv;

GaussianExtractorRGB::GaussianExtractorRGB() {
}

GaussianExtractorRGB::GaussianExtractorRGB(const GaussianExtractorRGB& orig) {
}

GaussianExtractorRGB::~GaussianExtractorRGB() {
}

void GaussianExtractorRGB::operator()(IplImage* image, cv::Mat_<double>* mean, cv::Mat_<double>* var) {
    std::vector<Mat_<double> > obs;
    for (int i = 0; i < image->width; i++) {
        for (int j = 0; j < image->height; j++) {
            uchar red = ((uchar*) (image->imageData + image->widthStep * j))[i * image->nChannels];
            uchar green = ((uchar*) (image->imageData + image->widthStep * j))[i * image->nChannels + 1];
            uchar blue = ((uchar*) (image->imageData + image->widthStep * j))[i * image->nChannels + 2];
            if (red!= 255 && blue != 255 && green!=0) {
                Mat_<double> f(image->nChannels, 1);
                f(0) = ((double) red) / 255.0;
                f(1) = ((double) green) / 255.0;
                f(2) = ((double) blue) / 255.0;
                obs.push_back(f);
            }
        }
    }
    Mat_<double>* samples = new Mat_<double>[obs.size()];
    for(int i = 0; i < obs.size(); ++i){
        samples[i] = obs[i];
    }
    calcCovarMatrix(samples, obs.size(), *var, *mean, COVAR_NORMAL);
    delete[] samples;
}